package com.junt.library;

import android.app.Activity;
import android.content.Intent;

import com.junt.library.core.QueryType;
import com.junt.library.interfaces.ImageEngine;
import com.junt.library.interfaces.OnMediaSelectedListener;
import com.junt.library.model.Media;
import com.junt.library.model.MediaFormats;
import com.junt.library.view.DirectoryActivity;
import com.junt.library.view.MediaActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * 统一入口
 */
public class MediaSelector {
    private static MediaSelector mediaSelector;
    /**
     * 图片加载器
     */
    private ImageEngine imageEngine;
    /**
     * 查询类型
     */
    private QueryType queryType = QueryType.IMAGE;
    /**
     * 最多选择的数量
     */
    private int maxSelected = 9;
    /**
     * 文件尺寸过滤，200(kb) = 200*1000 (b)
     */
    private long minSize = 200 * 1000;

    /**
     * 文件后缀，用于过滤
     */
    private String mediaFormats = MediaFormats.NONE;
    /**
     * 选择回调
     */
    private OnMediaSelectedListener listener;

    public static MediaSelector getInstance() {
        if (mediaSelector == null) {
            mediaSelector = new MediaSelector();
        }
        return mediaSelector;
    }


    /**
     * 跳转选择
     *
     * @param context  调用方Activity
     * @param listener 选择回调
     */
    public void obtain(Activity context, OnMediaSelectedListener listener) {
        this.listener = listener;
        context.startActivity(new Intent(context, MediaActivity.class));
    }

    /**
     * 跳转选择
     * 跳转至按目录分类界面
     *
     * @param context  调用方Activity
     * @param listener 选择回调
     */
    public void obtainDir(Activity context, OnMediaSelectedListener listener) {
        this.listener = listener;
        context.startActivity(new Intent(context, DirectoryActivity.class));
    }

    /**
     * 重置初始设置
     */
    private void reset() {
        imageEngine = null;
        queryType = QueryType.IMAGE;
        maxSelected = 9;
        minSize = 200_000;
        listener = null;
        mediaFormats = "";
    }

    /**
     * 返回选择结果
     *
     * @param activity MediaActivity  、DirActivity
     * @param medias     选择的文件集合
     */
    public void setResult(Activity activity, List<Media> medias) {
        activity.finish();
        listener.onMediaSelected(medias);
        reset();
    }

    /**
     * 返回无结果（取消）
     *
     * @param activity MediaActivity  、DirActivity
     */
    public void setNoneResult(Activity activity) {
        activity.finish();
        reset();
    }

    public ImageEngine getImageEngine() {
        return imageEngine;
    }

    public MediaSelector setImageEngine(ImageEngine imageEngine) {
        this.imageEngine = imageEngine;
        return this;
    }

    public QueryType getQueryType() {
        return queryType;
    }

    public MediaSelector setQueryType(QueryType queryType) {
        this.queryType = queryType;
        return this;
    }

    public int getMaxSelected() {
        return maxSelected;
    }

    public MediaSelector setMaxSelected(int maxSelected) {
        this.maxSelected = maxSelected;
        return this;
    }

    public long getMinSize() {
        return minSize;
    }

    /**
     * 过滤文件尺寸
     *
     * @param minSize kb
     * @return MediaSelector
     */
    public MediaSelector setMinSize(long minSize) {
        this.minSize = minSize * 1000;
        return this;
    }

    public String getMediaFormats() {
        return mediaFormats == null ? "" : mediaFormats;
    }

    public MediaSelector setMediaFormats(String mediaFormats) {
        this.mediaFormats = mediaFormats;
        return this;
    }
}
